use std::sync::mpsc::{channel, Sender};
use std::sync::{Arc, Mutex};
use std::thread;

pub struct SimpleQueue<T: Send + Sync + 'static> {
    listeners: Arc<Mutex<Vec<SimpleQueueListener<T>>>>,
}
impl<T: Send + Sync + 'static> SimpleQueue<T> {
    pub fn builder() -> Self {
        Self {
            listeners: Arc::new(Mutex::new(Vec::new())),
        }
    }
    pub fn add_listen(&self, name: &str, listener: fn(message: T)) -> Self {
        let (tx, rx) = channel::<T>();
        let running = Arc::new(std::sync::atomic::AtomicBool::new(true));
        // 启动监听线程
        thread::spawn(move || {
            loop {
                // 阻塞等待消息
                match rx.recv() {
                    Ok(message) => {
                        listener(message);
                    }
                    Err(_) => {
                        // 通道关闭，退出循环
                        break;
                    }
                }
                // 检查是否需要停止
                if !running.load(std::sync::atomic::Ordering::Relaxed) {
                    break;
                }
            }
        });
        let mut listeners = self.listeners.lock().unwrap();
        listeners.push(SimpleQueueListener {
            name: name.to_string(),
            sender: tx,
        });
        self.clone()
    }
    pub fn send(&self, name: &str, message: T) {
        let listeners = self.listeners.lock().unwrap();
        if let Some(listener) = listeners.iter().find(|l| l.name == name) {
            // 非阻塞发送消息
            let _ = listener.sender.send(message);
        }
    }
}
pub struct SimpleQueueListener<T: Send + Sync + 'static> {
    name: String,
    sender: Sender<T>,
}

impl<T: Send + Sync + 'static> Clone for SimpleQueue<T> {
    fn clone(&self) -> Self {
        Self {
            listeners: self.listeners.clone(),
        }
    }
}
